// const person = { name: '杨英承', age: 27 }
//
// const proxyPerson = new Proxy(person, {
//     get(target, key, receiver) {
//         return Reflect.get(receiver, key);// 相当于 receiver[key]
//     },
//     set(target, key, value, receiver) {
//         Reflect.set(receiver, key, value) // 相当于 receiver[key] = value
//     }
// })
//
// console.log(proxyPerson.name)
//
// proxyPerson.name = 'yyc'
// 会直接报错，栈内存溢出 Maximum call stack size exceeded

// 正确示例
//
const person = { name: '杨英承', age: 27 }
const proxyPerson = new Proxy(person, {
    get(target, key, receiver) {
        return Reflect.get(target, key, receiver)
    },
    set(target, key, value, receiver) {
        Reflect.set(target, key, value, receiver)
    }
})

console.log(proxyPerson.name) // 杨英承

proxyPerson.name = 'yyc'

console.log(proxyPerson.name) // yyc


// ================== 这么写可以但不建议 ==================


const person = { name: '杨英承', age: 27 }
const proxyPerson = new Proxy(person, {
    get(target, key, receiver) {
        return Reflect.get(target, key);// 相当于 receiver[key]
    },
    set(target, key, value, receiver) {
        Reflect.set(target, key, value) // 相当于 receiver[key] = value
    }
})

console.log(proxyPerson.name)
proxyPerson.name = 'yyc'
console.log(proxyPerson.name)

const person = { name: '杨英承', age: 27 }
const proxyPerson2 = new Proxy(person, {
    get(target, key, receiver) {
        return target[key];// 相当于 receiver[key]
    },
    set(target, key, value, receiver) {
        target[key] = value// 相当于 receiver[key] = value
    }
})

console.log(proxyPerson2.name)
proxyPerson2.name = 'yyc'
console.log(proxyPerson2.name)